*******************************************************************
***		DoFile to predict establishments' earnings situation	***
*******************************************************************
* Analyseeinheit: Betriebe (Aggregation erfolgt in DoFile 03, ganz unten)
	* aber keine Ergebnisausgabe fuer einzelne Betriebe
* Regressionsergebnisse beruhen auf N>10000
* Auszaehlungen beruhen auf N>700

set more off
log using "$log/04_earnsit_rint_is.log", replace

************************
*** data preparation ***
************************
use "$orig/deu_gdpchange_90-14.dta", clear
	gen gdpchange2 = gdpchange ^ 2
	replace gdpchange2 = gdpchange2 * -1 if gdpchange < 0
	rename jahr year

merge 1:1 year using "$orig/trade_93-12.dta"
	drop if _merge != 3
	drop _merge

merge 1:1 year using "$orig/macrovars_deuiips_85-14_b.dta", nogen
	rename year jahr
	sort jahr

*** compute counterfactual trends absent the euro ***
* create counterfactual values for post-1995/1998 *
foreach var of varlist rint_deuiips rint_deuis {
	gen `var'_cf = (`var'[1] + `var'[2] + `var'[3] + `var'[4] + `var'[5] + `var'[6] + `var'[7] + `var'[8] + `var'[9] + `var'[10]) / 10		/* counterfactual is avg value 1985-1995 */
	replace `var'_cf = `var' if jahr <= 1995
	}

* merge with establishments
qui merge 1:m jahr using "$data/estsample_1993-2008.dta"
	drop if jahr > 2008 | jahr < 1993
	drop _merge

* generate year-dummies
forvalues year = 1993/2008 {
	gen y`year' = 0
	qui replace y`year' = 1 if jahr==`year'
	}

* define macros
global years "y1993-y2007"	/* ref category is 2008*/
global industry "landwi verarb bauwi handel verkuit kredit dieleist"

* generate interaction terms
foreach ind of varlist $industry {
	foreach var of varlist rint_deuis delta_trade_swech {
		qui gen `ind'x`var' = `ind' * `var'
		}
	}

global inddums "verarb verkuit landwi handel kredit bauwi" /* refcat is dieleist */
global indxrint_deuis "verarbxrint_deuis verkuitxrint_deuis landwixrint_deuis handelxrint_deuis kreditxrint_deuis bauwixrint_deuis" /* refcat is dieleist */
global indxdelta_trade_swech "verarbxdelta_trade_swech verkuitxdelta_trade_swech landwixdelta_trade_swech handelxdelta_trade_swech kreditxdelta_trade_swech bauwixdelta_trade_swech" /* refcat is dieleist */
global indxdollarindex "verarbxdollarindex verkuitxdollarindex landwixdollarindex handelxdollarindex kreditxdollarindex bauwixdollarindex" /* refcat is dieleist */

****************
*** analysis ***
****************
bysort estid: gen wavenr = _n
svyset estid[pw=weight_size]
bysort estid: gen totalwaves = _N

*** describe export share of sales by industry ***
matrix define exportshare = J(7, 10, .)
matrix rownames exportshare = $industry
matrix colnames exportshare = mean93 se cilb ciub N mean08 se cilb ciub N

local count = 0
qui foreach ind of varlist $industry	{
	local ++count
	qui mean export_proz [pw=weight_size] if (jahr ==1993 & `ind' == 1)
	matrix table = r(table)
	matrix exportshare[`count', 1] =  table[1,1]
	matrix exportshare[`count', 2] =  table[2,1]
	matrix exportshare[`count', 3] =  table[5,1]
	matrix exportshare[`count', 4] =  table[6,1]
	matrix exportshare[`count', 5] =  e(N)

	qui mean export_proz [pw=weight_size] if (jahr ==2008 & `ind' == 1)
	matrix table = r(table)
	matrix exportshare[`count', 6] =  table[1,1]
	matrix exportshare[`count', 7] =  table[2,1]
	matrix exportshare[`count', 8] =  table[5,1]
	matrix exportshare[`count', 9] =  table[6,1]
	matrix exportshare[`count',10] =  e(N)
	}

matrix define exportshare_uw = J(7, 10, .)
matrix rownames exportshare_uw = $industry
matrix colnames exportshare_uw = mean93 se cilb ciub N mean08 se cilb ciub N

local count = 0
qui foreach ind of varlist $industry	{
	local ++count
	mean export_proz if (jahr ==1993 & `ind' == 1)
	matrix table = r(table)
	matrix exportshare_uw[`count', 1] =  table[1,1]
	matrix exportshare_uw[`count', 2] =  table[2,1]
	matrix exportshare_uw[`count', 3] =  table[5,1]
	matrix exportshare_uw[`count', 4] =  table[6,1]
	matrix exportshare_uw[`count', 5] =  e(N)

	mean export_proz [pw=weight_size] if (jahr ==2008 & `ind' == 1)
	matrix table = r(table)
	matrix exportshare_uw[`count', 6] =  table[1,1]
	matrix exportshare_uw[`count', 7] =  table[2,1]
	matrix exportshare_uw[`count', 8] =  table[5,1]
	matrix exportshare_uw[`count', 9] =  table[6,1]
	matrix exportshare_uw[`count',10] =  e(N)
	}

* Im Folgenden ist der durchschnittl. Exportanteil am Umsatz nach Branche dargestellt für die Jahre 1993 und 2008
* berechnet aus einer Regression auf >15.000 Betrieben, bzw. >60.000 Betriebsjahren.
tabulate w73_1
display "$industry"
matrix list exportshare
display "$industry"
matrix list exportshare_uw

/*
	Variablenbeschreibung fuer die folgenden Analysen:
	ertragslage_l0: Ertragslage eines Betriebs im jeweiligen Jahr
	rint_deuis: relative interest rate im jeweiligen Jahr fuer Deutschland
		im Verhueltnis zu rint in diesem Jahr fuer Italien, Irland, Portugal, Spanien
	indxdelta_trade_swech: Wachstum des Handelsvolumens zwischen Schweden, Schweiz und China, Osteuropa in diesem Jahr
	verarb, verkuit, etc.: Branchendummies
*/
bysort estid jahr: gen indic=_N
tab indic
drop indic

reghdfe ertragslage_l0 $indxrint_deuis rint_deuis gdpchange gdpchange2 $indxdelta_trade_swech delta_trade_swech if jahr <=2008 [pw=weight_size], abs(estid) keepsin vce(cluster ind_year estid)
est sto areg
gen areg_sample = e(sample)


* matrix to display marginal effects by industry with sampling variance *
matrix define earnsit_lincom_rintdeuis = J(7, 6, .)
matrix colnames earnsit_lincom_rintdeuis = 1_w73_1 2_rint_coef 3_rint_se 4_rint_cilb 5_rint_ciub 6_N

local count = 0
foreach ind of varlist landwi verarb bauwi handel verkuit kredit {
	local ++count
	dis " "
	dis " "
	dis "`ind':"
	est restore areg
	qui lincomest rint_deuis+`ind'xrint_deuis
	matrix table = r(table)
	matrix earnsit_lincom_rintdeuis [`count', 1] = `count'	/* gives numlabel of w73_1 */
	matrix earnsit_lincom_rintdeuis [`count', 2] = table[1,1]
	matrix earnsit_lincom_rintdeuis [`count', 3] = table[2,1]
	matrix earnsit_lincom_rintdeuis [`count', 4] = table[5,1]
	matrix earnsit_lincom_rintdeuis [`count', 5] = table[6,1]
	qui sum `ind' if `ind'==1 & areg_sample == 1									/* for N */
	matrix earnsit_lincom_rintdeuis [`count', 6] = r(N)
	}
	est restore areg	/* slightly different procedure for the reference group dieleist */
	qui lincomest rint_deuis
	matrix table = r(table)
	matrix earnsit_lincom_rintdeuis [7, 1] = 7	/* gives numlabel of w73_1 */
	matrix earnsit_lincom_rintdeuis [7, 2] = table[1,1]
	matrix earnsit_lincom_rintdeuis [7, 3] = table[2,1]
	matrix earnsit_lincom_rintdeuis [7, 4] = table[5,1]
	matrix earnsit_lincom_rintdeuis [7, 5] = table[6,1]
	qui sum dieleist if dieleist==1 & areg_sample == 1	/* for N */
	matrix earnsit_lincom_rintdeuis [7, 6] = r(N)

*** Die folgende Tabelle stellt Schaetzergebnisse aus der obigen (Zeile 142) Regression dar ***
matrix list earnsit_lincom_rintdeuis

*** generate counterfactual trends
est restore areg
gen deltarint = rint_deuis - rint_deuis_cf if jahr <= 2008 			/* true value minus pre-euro value: */
																	/* rint_deuis was deltarint HIGHER than it would have been without the euro */

gen effecton_earn = _b[rint_deuis] * deltarint ///
					+ _b[verarbxrint_deuis] * verarb * deltarint ///
					+ _b[verkuitxrint_deuis] * verkuit * deltarint ///
					+ _b[landwixrint_deuis] * landwi * deltarint ///
					+ _b[handelxrint_deuis] * handel * deltarint ///
					+ _b[kreditxrint_deuis] * kredit * deltarint ///
					+ _b[bauwixrint_deuis] * bauwi * deltarint if jahr <= 2008


*** compute avg. earnsit by industry-year (for later use in wage regression)
gen earnsit_ind_l0 = .
sort estid
svyset estid[pw=weight_size]
foreach num of numlist 1/7 {
	foreach jahr of numlist 1993/2008 {
		qui svy: mean ertragslage_l0 if w73_1 == `num' & jahr == `jahr'
		qui replace earnsit_ind_l0 = _b[ertragslage_l0] if w73_1 == `num' & jahr == `jahr'
		}
	}

* same, but lagged *
gen earnsit_ind_l1 = .
foreach num of numlist 1/7 {
	foreach jahr of numlist 1994/2008 {
		qui svy: mean ertragslage_l0 if w73_1 == `num' & jahr == `jahr' - 1	 				/* compute avg. from previous year */
		qui replace earnsit_ind_l1 = _b[ertragslage_l0] if w73_1 == `num' & jahr == `jahr' /* plug that value into lagvar of this year */
		}
	}

gen earnsit_ind_l2 = .
foreach num of numlist 1/7 {
	foreach jahr of numlist 1995/2008 {
		qui svy: mean ertragslage_l0 if w73_1 == `num' & jahr == `jahr' - 2	 				/* compute avg. from previous year */
		qui replace earnsit_ind_l2 = _b[ertragslage_l0] if w73_1 == `num' & jahr == `jahr' /* plug that value into lagvar of this year */
		}
	}

****************************************************************************************************
***		plot observed and counterfactual trends over time for manufacturing and construction	 ***
****************************************************************************************************
matrix define cftrends = J(16, 7, .)
matrix colnames cftrends = year verarb_obs verarb_cf N_verarb bauwi_obs bauwi_cf N_bauwi

gen cf_earnsit = ertragslage_l0 - effecton_earn
svyset estid[pw=weight_size]
local yrcount = 0
foreach num of numlist 1993/2008	{
		local ++yrcount
		matrix cftrends [`yrcount', 1] = `num'
		local indcount = 0

		foreach ind of varlist verarb bauwi	{
				local ++indcount
				qui svy: mean ertragslage_l0 if jahr == `num' & `ind' == 1
				matrix cftrends [`yrcount', ((`indcount' - 1)*3 + 2)] = _b[ertragslage_l0]		/* observed avg. earnsit by industry and year */
				qui svy: mean cf_earnsit if jahr == `num' & `ind' == 1
				matrix cftrends [`yrcount', ((`indcount' - 1)*3 + 3)] = _b[cf_earnsit]		/* counterfactual earnsit by industry and year */
				matrix cftrends [`yrcount', ((`indcount' - 1)*3 + 4)] = e(N)
			}
		}
*	Die folgende Tabelle stellt fuer zwei Branchen den tatsaechlichen und kontrafaktischen Trend der Ertragslage dar (verarb.Gewerbe, Bauwirtschaft)
matrix list cftrends


***************************************************************************************************
***		effects on earnings situation for all industries for 2004 and two previous years		***
***************************************************************************************************
matrix define indearn04 = J(7, 5, .)
matrix colnames indearn04 = industry obs cf delta N

foreach num of numlist 1/7 {
		matrix indearn04 [`num', 1] = `num'
		qui svy: mean ertragslage_l0 if jahr==2004 & w73_1==`num'
		matrix indearn04 [`num', 2] = _b[ertragslage_l0]
		qui svy: mean cf_earnsit if jahr==2004 & w73_1==`num'
		matrix indearn04 [`num', 3] = _b[cf_earnsit]
		matrix indearn04 [`num', 4] = indearn04[`num', 2] - indearn04[`num', 3]
		matrix indearn04 [`num', 5] = e(N)
	}

matrix define indearn03 = J(7, 4, .)
matrix colnames indearn03 = industry obs cf delta

foreach num of numlist 1/7 {
		matrix indearn03 [`num', 1] = `num'
		qui svy: mean ertragslage_l0 if jahr==2003 & w73_1==`num'
		matrix indearn03 [`num', 2] = _b[ertragslage_l0]
		qui svy: mean cf_earnsit if jahr==2003 & w73_1==`num'
		matrix indearn03 [`num', 3] = _b[cf_earnsit]
		matrix indearn03 [`num', 4] = indearn03[`num', 2] - indearn03[`num', 3]
	}

matrix define indearn02 = J(7, 4, .)
matrix colnames indearn04 = industry obs cf delta

foreach num of numlist 1/7 {
		matrix indearn02 [`num', 1] = `num'
		qui svy: mean ertragslage_l0 if jahr==2002 & w73_1==`num'
		matrix indearn02 [`num', 2] = _b[ertragslage_l0]
		qui svy: mean cf_earnsit if jahr==2002 & w73_1==`num'
		matrix indearn02 [`num', 3] = _b[cf_earnsit]
		matrix indearn02 [`num', 4] = indearn02[`num', 2] - indearn02[`num', 3]
	}

matrix define earndelta04 = J(7, 5, .)
matrix colnames earndelta04 = w73_1 delta04_l0 delta04_l1 delta04_l2 N

foreach num of numlist 1/7 {
		matrix earndelta04 [`num', 1] = `num'
		matrix earndelta04 [`num', 2] = indearn04[`num', 4]
		matrix earndelta04 [`num', 3] = indearn03[`num', 4]
		matrix earndelta04 [`num', 4] = indearn02[`num', 4]
		matrix earndelta04 [`num', 5] = indearn04[`num', 5]
	}

* 	Die folgende Tabelle beschreibt für das Jahr 2004 gegenwärtige und vergangene (gelagte) Effekte des Euro auf die Ertragslage.
matrix list earndelta04

************************************************
*** plug effecton_* back into linked dataset ***
************************************************
keep estid jahr weight_* consnrftempl earnsit_ind_*
merge 1:m estid jahr using "$data/linked_imputed_1993-2008.dta"
	drop _merge
sort estid jahr persnr
order estid jahr persnr
compress
save "$data/linked_imputed_1993-2008.dta", replace
tab jahr
log close
*** END ***

























